<template>
    <div>
        <Modal title="重置密码" @on-visible-change="changeStatusHandle" width="400" v-model="isShowRestPwdModal" @on-cancel="closeModal" @on-ok="confirmModal"  :mask-closable="false" :closable="false">
            <Form ref="formInline" :model="formInline" :rules="ruleInline" :label-width="80">
                <FormItem label="用户名：" prop="username">
                    <Input type="text" v-model="formInline.username" placeholder="请输入用户名..."/>
                </FormItem>
                <FormItem label="证件号：" prop="idcardNo">
                    <Input type="text" v-model="formInline.idcardNo" placeholder="请输入证件号..."/>
                </FormItem>
            </Form>
            <div slot="footer" style="text-align: center">
                <Button @click="closeModal">取消</Button>
                <Button type="primary" @click="confirmModal('formInline')">确定</Button>
            </div>
        </Modal>
    </div>
</template>

<script lang="tsx">
import { Component,Prop} from "vue-property-decorator"
import { SYSUSER_RESTPWD,INFO,SAVE } from '@/store/modules/crud/keys'
import MiXinModal  from '@/mixin/views/modal'
import { Base64 } from 'js-base64';

@Component
export default class TheRegisterModal extends MiXinModal {
    @Prop(Boolean)isShowRestPwdModal!: boolean

    formInline = {
        username: '',
        idcardNo: ''
    }
    ruleInline= {
        username:[{ required: true, message: '用户名不能为空.', trigger: 'blur' }],
         idcardNo: [
            { required: true, message: '证件号码不能为空.', trigger: 'blur' },
            { pattern: /^[1-9]\d{1}(.{13}|.{16})$/, message: "身份证格式不正确", trigger: "blur"}
        ]
    }
    get _name(){
        return SYSUSER_RESTPWD
    }
    closeModal(){
        this.$emit("close-restpwd-modal");
    }
    confirmModal(formInline: any){
        const username = this.formInline.username;
        const idcardNo = this.formInline.idcardNo;
        (this.$refs[formInline] as any).validate(async (valid: any) => {
            if (valid) {
                const key = this.generatekey();
                
                const base64Username = Base64.encode(username);
                const base64IdcardNo = Base64.encode(idcardNo);

                const desUsername = this.encryptDes(base64Username,key);
                const desIdcardNo = this.encryptDes(base64IdcardNo,key);

                const checkBit = this.generateCheckBit(username+idcardNo+key);
                this.$store.commit(INFO,{name: this._name,info: {
                    username: Base64.encode(desUsername+key),
                    idcardNo: Base64.encode(desIdcardNo),
                    checkBit: checkBit
                }});

                const promise = await this.$store.dispatch(SAVE,{name: this._name});
                if(promise.data==1){
                    this.$Message.success("重置成功.");
                     this.closeModal();
                    return;
                }
                if(promise == undefined){
                    this.$Message.error("重置失败!");
                }
            }
        })
    }
    changeStatusHandle(){
        if(this.isShowRestPwdModal){
            this.formInline.username = ''
            this.formInline.idcardNo = ''
        }
    }
}
</script>

<style scoped>

</style>
